import { AsyncStorage } from 'react-native';
import T from "../utils/T";
import Request from "../utils/Request";
const { Toast } = require('antd-mobile');
const STORAGE_KEY = "STORAGE_KEY_search";
export default {
    namespace: 'search',
    state: {
        loading: false,
        activities: [],
        companies: [],
        groups: [],
        member: []
    },
    reducers: {
        save(state, { payload }) {
            return Object.assign({}, state, payload);
        }
    },
    effects: {
        *saveToStorage({ payload }, { call }) {
            yield call(AsyncStorage.setItem, STORAGE_KEY, JSON.stringify(payload));
        },
        *restoreFromStorage({ payload }, { call, put, select }) {
            const data = yield call(AsyncStorage.getItem, STORAGE_KEY);
            yield put({
                type: 'save',
                payload: Object.assign({}, JSON.parse(data))
            });
        },
        *fetchData({ payload }, { call, put, select }) {
            Toast.loading(T('loading'));
            const auth = yield select(state => state.auth);
            payload['Authorization'] = `${auth.token_type} ${auth.access_token}`;
            const res = yield call(Request.get, Request.getApi().search, payload);
            Toast.hide();
            if (res) {
                yield put({
                    type: 'save',
                    payload: Object.assign({}, res)
                });
            }
        },
    },
    subscriptions: {
        setup({ dispatch }) {
        }
    },
};
